#include <REG51.H>
#include <Timing.h>
#include <Clock.h>
#include <Key.h>
#include <Mode.h>

// 调时位置
Timing_pos Timing_buff = Timing_pos_year;

/**
 * @brief  调时复位
 * @param  None
 * @retval None
 */
void Timing_reset(void)
{
    Timing_buff = Timing_pos_year;
}

/**
 * @brief  检查天数是否正确（存在）
 * @param  None
 * @retval None
 */
static void Timing_updata_day(void)
{
    if (Clock_buff.day > 28)
    {
        switch (Clock_buff.day)
        {
        case 29:
            if (Clock_buff.month == 2 && Is_leap_year() == 0)
            {
                Clock_buff.day = 1;
            }
            break;

        case 30:
            if (Clock_buff.month == 2)
            {
                Clock_buff.day = 1;
            }
            break;

        case 31:
            if (Clock_buff.month == 2 || Clock_buff.month == 4 || Clock_buff.month == 6 || Clock_buff.month == 9 || Clock_buff.month == 11)
            {
                Clock_buff.day = 1;
            }
            break;

        case 32:
            Clock_buff.day = 1;
            break;
        }
    }
}

/**
 * @brief  调时时数据验证
 * @param  operation: 操作，加（1），减（0）
 * @retval None
 */
static void Timing_updata(bit operation)
{
    switch (Timing_buff)
    {
    case Timing_pos_year:
        if (operation == 1)
        {
            if (Clock_buff.year < 99)
                Clock_buff.year++;
            else
                Clock_buff.year = 0;
        }
        else
        {
            if (Clock_buff.year > 0)
                Clock_buff.year--;
            else
                Clock_buff.year = 99;
        }
        Timing_updata_day();
        break;

    case Timing_pos_month:
        if (operation == 1)
        {
            if (Clock_buff.month < 12)
                Clock_buff.month++;
            else
                Clock_buff.month = 1;
        }
        else
        {
            if (Clock_buff.month > 1)
                Clock_buff.month--;
            else
                Clock_buff.month = 12;
        }
        if (Clock_buff.day > 28)
        {
            Timing_updata_day();
        }
        break;

    case Timing_pos_day:
        if (operation == 1)
        {
            Clock_buff.day++;
            Timing_updata_day();
        }
        else
        {
            if (Clock_buff.day == 1)
            {
                if (Clock_buff.month == 2 && Is_leap_year() == 0)
                {
                    Clock_buff.day = 28;
                }
                else if (Clock_buff.month == 2 && Is_leap_year() == 1)
                {
                    Clock_buff.day = 29;
                }
                else if (Clock_buff.month == 4 || Clock_buff.month == 6 || Clock_buff.month == 9 || Clock_buff.month == 11)
                {
                    Clock_buff.day = 30;
                }
                else if (Clock_buff.month == 1 || Clock_buff.month == 3 || Clock_buff.month == 5 || Clock_buff.month == 7 || Clock_buff.month == 8 || Clock_buff.month == 10 || Clock_buff.month == 12)
                {
                    Clock_buff.day = 31;
                }
            }
            else
            {
                Clock_buff.day--;
            }
        }
        break;

    case Timing_pos_hour:
        if (operation == 1)
        {
            if (Clock_buff.hour < 23)
                Clock_buff.hour++;
            else
                Clock_buff.hour = 0;
        }
        else
        {
            if (Clock_buff.hour > 0)
                Clock_buff.hour--;
            else
                Clock_buff.hour = 23;
        }

        break;

    case Timing_pos_minute:
        if (operation == 1)
        {
            if (Clock_buff.minute < 59)
                Clock_buff.minute++;
            else
                Clock_buff.minute = 0;
        }
        else
        {
            if (Clock_buff.minute > 0)
                Clock_buff.minute--;
            else
                Clock_buff.minute = 59;
        }
        break;

    case Timing_pos_second:
        if (operation == 1)
        {
            if (Clock_buff.second < 59)
                Clock_buff.second++;
            else
                Clock_buff.second = 0;
        }
        else
        {
            if (Clock_buff.second > 0)
                Clock_buff.second--;
            else
                Clock_buff.second = 59;
        }
        break;
    }
}

/**
 * @brief  调时控制
 * @param  None
 * @retval None
 */
void Timing_ctrl(void)
{
    Timing_buff = Timing_pos_year;

    while (Timing_buff != Timing_pos_complete)
    {
        switch (Key_Scan())
        {
        case Key0_Down:
            Timing_buff--;
            break;

        case Key1_Down:
            Timing_updata(1);
            break;

        case Key2_Down:
            Timing_updata(0);
            break;
        }
    }

    System_Mode = Mode_Clock;
}
